package com.itheima.web.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Dept;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.DeptService;
import com.itheima.service.impl.DeptServiceImpl;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/dept/*")
public class DeptServlet extends BaseServlet {
//    protected Result doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        //获取标记
//        String pathInfo = request.getPathInfo().substring(1);
//        //判断标记调用对应的方法
//        if ("findByPage".equals(pathInfo)){
//            //调用分页查询的方法
//            findByPage(request,response);
//        }else if ("save".equals(pathInfo)){
//            //调用新增的方法
//            save(request,response);
//        }else if ("findById".equals(pathInfo)){
//            //调用根据id查询信息的方法
//            findById(request,response);
//        }else if ("edit".equals(pathInfo)){
//            edit(request,response);
//        }else if ("delete".equals(pathInfo)){
//            delete(request,response);
//        }else if ("findAll".equals(pathInfo)){
//            findAll(request,response);
//        }
//    }
    private DeptService deptService = BeanFactory.getBean("deptService",DeptService.class);
    private Result findAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //调用service,根据id查询信息
            List<Dept> list = deptService.findAll();
            //响应给过到客户端
             return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
             return new Result(true,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }

    private Result delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1. 获取请求参数并封装
            String[] ids = BeanUtil.fillBeanFromJson(request, String[].class);
            //2.调用service层方法,编辑数据
            deptService.delete(ids);
            //3.响应结果
             return new Result(true, MessageConstant.DELETE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
             return new Result(false,MessageConstant.DELETE_COMPANY_FAIL);
        }
    }

    private Result edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1. 获取请求参数并封装
            Dept dept = BeanUtil.fillBeanFromJson(request, Dept.class);
            //2.调用service层方法,编辑数据
            deptService.update(dept);
            //3.响应结果
             return new Result(true,MessageConstant.EDIT_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
             return new Result(false,MessageConstant.EDIT_COMPANY_FAIL);
        }
    }

    private Result findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //获取请求参数 id
            String id = request.getParameter("id");
            //调用service,根据id查询信息
            Dept dept = deptService.findById(id);
            //响应给过到客户端
             return new Result(true,MessageConstant.QUERY_COMPANY_SUCCESS,dept);
        } catch (Exception e) {
            e.printStackTrace();
             return new Result(true,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }

    private Result save(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            //1. 获取请求参数并封装
            Dept dept = BeanUtil.fillBeanFromJson(request, Dept.class);
            //2.调用service层方法,保存数据
            deptService.save(dept);
            //3.响应结果
             return new Result(true,MessageConstant.SAVE_COMPANY_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
             return new Result(false,MessageConstant.SAVE_COMPANY_FAIL);
        }

    }

    private Result findByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            //1.获取请求参数 currentPage和pageSize
            String currentPageSrt = request.getParameter("currentPage");
            String pageSizeSrt = request.getParameter("pageSize");
            //2.非空校验并赋默认值
            int currentPage=1,pageSize=5;
            if (!StringUtils.isBlank(currentPageSrt)){
                currentPage = Integer.parseInt(currentPageSrt);
            }
            if (!StringUtils.isBlank(pageSizeSrt)){
                pageSize = Integer.parseInt(pageSizeSrt);
            }
            //3.调用service层方法,执行查询pageInfo
            PageInfo<Dept> info = deptService.findByPage(currentPage, pageSize);
            //4.响应结果
             return new Result(true, MessageConstant.QUERY_COMPANY_SUCCESS,info);
        } catch (Exception e) {
            e.printStackTrace();
             return new Result(false,MessageConstant.QUERY_COMPANY_FAIL);
        }
    }
}
